package com.library.admin.service;

import com.framework.Encrypt;
import com.framework.Page;
import com.library.admin.dao.AdminDaoImpl;
import com.library.entity.Administrator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class AdminServiceImpl {
	
	@Resource
	private AdminDaoImpl adminDaoImpl;

	public Administrator login(String username, String password){
		return this.adminDaoImpl.findByUsernameAndPwd(username, Encrypt.SHA512(password));
	}

	public Administrator getAdmin(Integer adminId){
		return this.adminDaoImpl.getAdmin(adminId);
	}

	public Administrator getAdmin(String username){
		return this.adminDaoImpl.getAdmin(username);
	}

	public void addAdmin(Administrator administrator){
		String password = Encrypt.SHA512(administrator.getPassword());
		administrator.setPassword(password);
		this.adminDaoImpl.saveAdmin(administrator);
	}

	/*更新管理员信息*/
	public void updateAdmin(Administrator admin){
		admin.setRole(admin.getRole());
		this.adminDaoImpl.updateAdmin(admin);
	}

	/*更新管理员密码*/
	public void updateAdminPassword(Administrator administrator){
		String password = Encrypt.SHA512(administrator.getPassword());
		administrator.setPassword(password);

		this.adminDaoImpl.updateAdmin(administrator);
	}

	/*删除指定管理员*/
	public void deleteAdmin(int adminId){
		Administrator ad = this.getAdmin(adminId);
		this.adminDaoImpl.deleteAdmin(ad);
	}

	/*删除多个管理员*/
	public void deleteAdminGroup(int[] admins){
		Administrator ad = null;
		for(int i = 0;i < admins.length;++i){
			ad = this.getAdmin(admins[i]);
			this.adminDaoImpl.deleteAdmin(ad);
		}
	}

	@Transactional(readOnly=true)
	/*查找全部管理员*/
	public List<Administrator> getAllAdmin(){
		Map paramsMap = new HashMap();
		paramsMap.put("rank",false);
		return this.adminDaoImpl.findAdminsByProperty(new HashMap());
	}

	@Transactional(readOnly=true)
	/*查找全部禁用的管理员*/
	public Page<Administrator> getPageByParam(int pageNum, int pageSize, Map paramsMap){
		return  this.adminDaoImpl.findPageByProperty(pageNum,pageSize,paramsMap);
	}
	@Transactional(readOnly=true)
	/*查找全部禁用的管理员*/
	public List<Administrator> getAllBlackAdmins(){
		Map paramsMap = new HashMap();
		paramsMap.put("state",true);
		paramsMap.put("rank",false);
		return this.adminDaoImpl.findAdminsByProperty(paramsMap);
	}
}
